#  Copyright (C) 2012 University of Southern California,
#                     Philip J. Uren, Andrew D. Smith
#
#  Authors: Philip J. Uren, Andrew D. Smith
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#  This file was modified from a Google Test component, the following is the
#  license for Google Test  
#
#  Copyright 2008, Google Inc.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following 
#				disclaimer in the documentation and/or other materials provided with 
#				the distribution.
#     * Neither the name of Google Inc. nor the names of its
# 			contributors may be used to endorse or promote products derived from
# 			this software without specific prior written permission.
#
# 			THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# 			"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# 			LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# 			A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# 			OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# 			SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# 			LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# 			DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# 			THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# 			(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# 			OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# SYNOPSIS:
#
#   make [all]  - Makes everything.
#   make clean  - Removes all files generated by make.
#   make test   - Run all tests

ifndef SMITHLAB_CPP
$(error Must define SMITHLAB_CPP variable)
endif

# Points to the root of Google Test, relative to where this file is.
GTEST_DIR = ../gtest

# All tests produced by this Makefile.
TESTS = TestPoisson TestZTP TestNegativeBinomial TestMatrix TestZTNB \
        TestReadBinner TestUtility TestFDR TestGenomicRegionAggregator

# Where to find the code..
USER_DIR = ../common
TEST_DIR = .
INCLUDE_DIRS = -I$(USER_DIR) -I$(SMITHLAB_CPP) 

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include
CPPFLAGS += $(INCLUDE_DIRS)

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra
ifeq "$(shell uname)" "Darwin"
CXXFLAGS += -arch x86_64
ifeq "$(shell if [ `sysctl -n kern.osrelease | cut -d . -f 1` -ge 13 ];\
              then echo 'true'; fi)" "true"
CXXFLAGS += -stdlib=libstdc++
endif
endif


# All Google Test headers.  Usually you shouldn't change this definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

######################### House-keeping build targets #########################
all : $(TESTS)

clean :
	@rm -f $(TESTS) gtest.a gtest_main.a *.o testlog.txt
.PHONY: clean

distclean :
	@rm Makefile regressionTests.xml
.PHONY: distclean

########################## How to build Google Test  ##########################
# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.

# Builds gtest.a and gtest_main.a.
# Usually you shouldn't tweak internal variables, indicated by a trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

gtest-all.o : $(GTEST_SRCS_)
	$(CXX) -DGTEST_HAS_PTHREAD=0 $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
	$(CXX) -DGTEST_HAS_PTHREAD=0 $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
	$(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
	$(AR) $(ARFLAGS) $@ $^

########################## How to build object files ##########################
# A test should link with either gtest.a or gtest_main.a, depending on whether 
# it defines its own main() function.
%.o : %.cpp %.hpp $(GTEST_HEADERS)  
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
	
Test%.o : $(TEST_DIR)/Test%.cpp $(USER_DIR)/%.hpp $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< 

############################ How to make each test ############################
LIBS=-lgsl -lgslcblas -lm -lpthread
TestMatrix : TestMatrix.o gtest_main.a $(USER_DIR)/Matrix.o \
             $(SMITHLAB_CPP)/smithlab_utils.o 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)

TestPoisson : TestPoisson.o gtest_main.a $(USER_DIR)/Poisson.o \
              $(USER_DIR)/SimpleXML.o 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)

TestZTP : TestZTP.o gtest_main.a $(USER_DIR)/ZTP.o $(USER_DIR)/SimpleXML.o 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)
	
TestNegativeBinomial : TestNegativeBinomial.o gtest_main.a \
                       $(USER_DIR)/NegativeBinomial.o $(USER_DIR)/SimpleXML.o \
                       $(SMITHLAB_CPP)/smithlab_utils.o 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)
	
TestZTNB : TestZTNB.o gtest_main.a \
           $(USER_DIR)/ZTNB.o $(USER_DIR)/SimpleXML.o \
           $(SMITHLAB_CPP)/smithlab_utils.o
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)
	
TestReadBinner : TestReadBinner.o gtest_main.a $(USER_DIR)/ReadBinner.o \
             $(SMITHLAB_CPP)/smithlab_utils.o $(SMITHLAB_CPP)/GenomicRegion.o 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)

TestUtility : TestUtility.o $(USER_DIR)/Utility.o \
							$(SMITHLAB_CPP)/GenomicRegion.o $(SMITHLAB_CPP)/smithlab_utils.o \
							gtest_main.a 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)

TestFDR : TestFDR.o $(USER_DIR)/FDR.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)

TestGenomicRegionAggregator : TestGenomicRegionAggregator.o \
					$(SMITHLAB_CPP)/GenomicRegion.o $(SMITHLAB_CPP)/smithlab_utils.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ $(LIBS)

########################## How to run all the tests ###########################
.PHONY : test
test : $(TESTS)
	@#first we make sure we've compiled all the executables we need..
	@make -C ../progs SMITHLAB_CPP=$(SMITHLAB_CPP) OPT=1 install;

	@echo
	@echo "##############################################"
	@echo "################# UNIT TESTS #################"
	@echo "##############################################"
	@echo
	@for f in $(TESTS); do ./$$f; done
	
	@# then we run the regression tests
	@echo
	@echo "##############################################"
	@echo "############## REGRESSION TESTS ##############"
	@echo "##############################################"
	@echo
	@python runRegressionTests.py regressionTests.xml
	@echo
	
